import { Injectable, Inject } from '@nestjs/common';
import { UserService } from 'src/user/user.service';
import { JwtService } from '@nestjs/jwt';

@Injectable()
export class AuthService {
  constructor(private readonly userService: UserService, private readonly jwtService: JwtService) { }

  async validateUser(username: string, pwd: string): Promise<any> {
    console.log(222);
    const user = await this.userService.findOneByUserName(username)
    console.log(user);
    if (user && user.password === pwd) {
      const { password, ...result } = user
      console.log(result);
      return result
    }

    return null
  }

  async login(user: any) {
    console.log(111);
    const payload = { username: user.username, sub: user.id };
    return {
      access_token: this.jwtService.sign(payload),
    };
  }
}
